﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;

using DotNetNuke;
using DotNetNuke.Common;
using DotNetNuke.Common.Utilities;
using DotNetNuke.Security;
using DotNetNuke.Services.Exceptions;
using DotNetNuke.Services.Localization;
using DotNetNuke.Services.FileSystem;
using DotNetNuke.Entities.Modules;
using DotNetNuke.Entities.Modules.Actions;
using DotNetNuke.UI.WebControls;

using CharityGamesOnline.Modules.Common;

namespace CharityGamesOnline.Modules.Account
{
    public partial class AccountEdit : CGOBase
    {
        private int accountUserID = 0;

        public int AccountUserID
        {
            get
            {
                if (accountUserID == 0)
                {
                    if ((Request.QueryString["UserID"] != null))
                    {
                        accountUserID = Int32.Parse(Request.QueryString["UserID"]);
                    }
                }
                return accountUserID;
            }
        }

        protected void Page_Load(object sender, EventArgs e)
        {
            try
            {
                cgoAccountLogView.AccountUserID = AccountUserID;
                cgoPaymentList.AccountUserID = AccountUserID;
                InitialScript();
                if (!Page.IsPostBack)
                {
                    BindData();
                }
            }

            catch (Exception exc)
            {
                //Module failed to load 
                Exceptions.ProcessModuleLoadException(this, exc);
            }
        }

        protected void cmdCancel_Click(object sender, EventArgs e)
        {
            try
            {
                Response.Redirect(Globals.NavigateURL(), true);
            }
            catch (Exception exc)
            {
                //Module failed to load 
                Exceptions.ProcessModuleLoadException(this, exc);
            } 
        }

        protected void cmdUpdate_Click(object sender, EventArgs e)
        {
            try
            {
                AccountController ctrlAccount = new AccountController();
                AccountInfo objAccount = new AccountInfo();
                objAccount.UserID = Int32.Parse(lbUserID.Text);
                objAccount.PortalID = PortalId;
                objAccount.Token = Decimal.Parse(txtToken.Text);                
                ctrlAccount.SaveAccount(objAccount);

                AccountLogInfo objLog = new AccountLogInfo(objAccount);
                objLog.Comment = txtComment.Text;
                ctrlAccount.SaveAccountLog(objLog);

                Response.Redirect(Globals.NavigateURL(), true);
            }
            catch (Exception exc)
            {
                //Module failed to load 
                Exceptions.ProcessModuleLoadException(this, exc);
            }
 
        }

        protected void cmdDelete_Click(object sender, EventArgs e)
        {
 
        }

        private void BindData()
        {
            if (AccountUserID > 0)
            {
                AccountController ctrlAccount = new AccountController();
                AccountInfo objAccount = ctrlAccount.GetAccount(PortalId, AccountUserID);
                if (!Null.IsNull(objAccount))
                {
                    lbUserID.Text = objAccount.UserID.ToString();
                    lbUserName.Text = objAccount.UserName;
                    txtToken.Text = objAccount.Token.ToString();
                }
            }
        }

        private void InitialScript()
        {
            string csName = string.Empty;
            string csText = string.Empty;
            Type csType = System.Reflection.MethodBase.GetCurrentMethod().GetType();

            csName = "ExtValidationScriptFile";
            csText = "<script src=\"" + ResolveUrl("Script/Validation.js") + "\" type=\"text/javascript\"></script>";
            if (!Page.ClientScript.IsClientScriptBlockRegistered(csName))
            {
                Page.ClientScript.RegisterClientScriptBlock(csType, csName, csText);
            }
        }
    }
}